﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Presenter.Support
{
    public abstract class BaseApplicationPage : LayoutsPageBase
    {
        #region Properties

        public bool IsDlg {
            get { return !string.IsNullOrEmpty(Page.Request.QueryString["IsDlg"]) && Page.Request.QueryString["IsDlg"] == "1" ? true : false; }
        }

        #endregion


        #region OverrideMethods

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

        }

        #endregion

        public void HandleError(Exception e)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", e.Message.Replace("\r", " ").Replace("\n", "<br/>").Replace("'", "&quot;")));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'red');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;}, 6000);}, 'sp.js');");
            javascript.Append(";}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "errorscript", javascript.ToString());
            //Logger.LogException(e, Microsoft.SharePoint.Administration.EventSeverity.Error,
            //   SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.RUNTIME);
        }

        public void HandleError(Exception e, WebControl[] disableControls, bool closeModal)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", e.Message.Replace("\r", " ").Replace("\n", "<br/>").Replace("'", "&quot;")));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'red');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            //if (closeModal) { javascript.Append("SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK);"); }
            //javascript.Append("}, 6000);}, 'sp.js');");
            javascript.Append(";}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "successscript", javascript.ToString());
            //Logger.LogException(e, Microsoft.SharePoint.Administration.EventSeverity.Error,
            //SPDiagnosticsAreas.SHAREPOINTFOUNDATION, SPDiagnosticsAreas.SharepointFoundationDiagnosticsCategories.RUNTIME);
            if (disableControls == null) return;
            foreach (var control in disableControls)
            {
                control.Enabled = false;
            }
        }

        public void HandleSuccess(string message, WebControl[] disableControls)
        {
            HandleSuccess(message, disableControls, false);
        }

        public void HandleSuccess(string message, WebControl[] disableControls, bool closeModal)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", message));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'green');");           
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            if (closeModal) { javascript.Append("SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK);"); }
            javascript.Append("}, 'sp.js');");
            //javascript.Append("}, 6000);}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "successscript", javascript.ToString());
            if (disableControls == null) return;
            foreach (var control in disableControls)
            {
                control.Enabled = false;
            }
        }

        public void HandleSuccessRedirect(string message, WebControl[] disableControls, bool gotoUserLibary, bool isModal)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", message));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'green');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            if (gotoUserLibary && isModal) { javascript.Append("SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK);"); }
            else if (gotoUserLibary) { javascript.Append(string.Format("window.location='{0}';", ListHelper.CurrentUserLibraryUrl)); }
            javascript.Append("}, 6000);}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "successscript", javascript.ToString());
            if (disableControls == null) return;
            foreach (var control in disableControls)
            {
                control.Enabled = false;
            }
        }

        public void HandleSuccessRedirect(string message, WebControl[] disableControls, bool gotoUserLibary)
        {
            HandleSuccessRedirect(message, disableControls, gotoUserLibary, false);
        }


        public void HandleWarning(string message)
        {
            StringBuilder javascript = new StringBuilder(@"<script type='text/javascript'>");
            javascript.Append("ExecuteOrDelayUntilScriptLoaded(function(){SP.UI.Status.removeStatus(statusID); statusID=null;");
            javascript.Append("var statusID = '';");
            javascript.Append(string.Format("statusID=SP.UI.Status.addStatus('', '{0}');", message));
            javascript.Append("SP.UI.Status.setStatusPriColor(statusID, 'yellow');");            
            javascript.Append("$('#s4-workspace').animate({ scrollTop: 0 });");
            //javascript.Append("setTimeout(function(){SP.UI.Status.removeStatus(statusID); statusID=null;}, 6000);}, 'sp.js');");
            javascript.Append(";}, 'sp.js');");
            javascript.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "successscript", javascript.ToString());
        }

    }

}
